#include "zf_common_headfile.h"
#ifndef CODE_SEARCH_LINE_H_
#define CODE_SEARCH_LINE_H_


#define STOP_ROW 2 //search stop line 搜先停止行
#define LSC_STEP 3 //longest white every step column
#define LSR_STEP 2 //every 5 row longest white
#define COL_STEP 5 //find line compare step  last value:5
#define ROW_STEP 2 //find line compare step 
#define JUMP_STEP 3 //every 5 column and every 5 row longest white


#define WHITEMAXMUL 13 //the max white value relative to reference white point bigger ratio 10 mean not bigger
#define WHITEMINMUL 8  //the min white value relative to reference white point smaller ratio 10 mean not smaller
#define REFERENCEROW 5 //reference rows statics
#define BLACKPOINT 100 //black point


#define    POINTS_MAX_LEN          120


#define SEARCHRANGE      10      //Search radius 
#define CONTRASTOFFEST   3      //搜线对比偏移

extern uint8_t iptsr[POINTS_MAX_LEN];//array of lines to the right of the longest white column   
extern uint8_t iptsr_num;
extern uint8_t iptsL[POINTS_MAX_LEN];//array of lines to the left of the longest white column   
extern uint8_t iptsL_num;
//拟合的中线
extern uint8_t  rpts[POINTS_MAX_LEN];
extern uint8_t rpts_num;
extern unsigned char   RowLose[POINTS_MAX_LEN];//丢线数组 1——不丢线 2——只丢右 3——只丢左 4——丢线

extern unsigned char white_col[COL];
void search_line(const uint8_t *img,uint8_t begin_y,uint8_t end_y);
void longest_column(uint8_t * img,uint8_t begin_col,uint8_t end_col);
extern unsigned char white_row[ROW];
void longest_row(const unsigned char *img,unsigned char begin_col,unsigned char end_row,unsigned char way);
void get_reference_point(const uint8_t * image);


#define     TC(img,x,y)       (img[(y)*COL+(x)]) //y代表行数  x代表列数 
extern unsigned char ipts0[POINTS_MAX_LEN][2] ;//左
extern unsigned char ipts1[POINTS_MAX_LEN][2] ;//右
extern unsigned char ipts0_num;
extern unsigned char ipts1_num;
extern unsigned char block_size;
extern unsigned char clip_value;

void find_line_r(unsigned char y1,unsigned char num);
void find_line_l(unsigned char y0,unsigned char num);
void findline_lefthand_adaptive(unsigned char *img, unsigned char block_size, unsigned char clip_value, unsigned char x, unsigned char y, unsigned char pts[][2], unsigned char *num);
void findline_righthand_adaptive(unsigned char *img, unsigned char block_size, unsigned char clip_value, unsigned char x, unsigned char y, unsigned char pts[][2], unsigned char *num);

unsigned char adapt_otsuThreshold(uint8_t *image,uint16_t row_num,unsigned char *threshold);  //注意计算阈值的一定要是原图像
#endif